'''
Coron Lattice Drawer
Author: Victor Codocedo H.
Universidad Tecnica Federico Santa Maria

Usage:
python coron_drawer.py lats/baclat.lat

baclat.lat is the output of LECO from Coron.
Add names, extension and stability options when executing LECO.
LECO is provided on Coron
'''
import vtk
import sys
import os
import sys
import numpy as np
import matplotlib
import networkx as nx
import pylab
import scipy




lattice = ""

if len(sys.argv)<2:
	print "\n*** usage:",sys.argv[0]," [latfile]"
	exit()
else:
	lattice = sys.argv[1]

f = open(lattice, "r")
counter = 0
lat={}
stabs = {}
parents={}
level={}
levels={}
maxlev = 0
for line in f:
	if not (line.startswith("#") or line.startswith("Top") or line.startswith("Bottom") or line.startswith("\n")):
		counter += 1
		if counter%4==1: # Info intent extent
			# Intent
			ida = line[line.index("{")+1:line.index("}")]
 			#print ida
			if ida != "":
				# Extent
				lat[ida] = line[line.index("[")+1:line.index("]")].split(", ")
				#print lat[ida]
				# Level
				lev = line[line.index("level=")+6:line.index("stability")].replace(", ","")
				stab = line[line.rfind("=")+1:]
				stab = float(stab.replace("]\n",""))
				stabs[ida] = stab
				lev = int(lev)
				level[ida] = lev
				if lev > maxlev:
					maxlev = lev
				if lev not in levels:
					levels[lev] = []
				levels[lev].append(ida)
			else:
				ida = "none"
				lat[ida] = "ALL"
				level[ida] = 0
				stabs[ida] = 1.0
				levels[0] = [ida]
		if counter%4==2: # Infor parents
			#if ida == "none":
			#	parents[ida] = []
			#else:
			if True:
				parents[ida] = []
				parent = line[line.index("[")+1:line.index("]")]
				parent = parent.split("}, ")
				for c in range(len(parent)):
					par = parent[c].replace("{","").replace("}","").strip()
					if par == "":
						parents[ida].append("none")
					else:
						parents[ida].append(par)
						#print stabs
matplotlib.pyplot.figure()
#h,bins=matplotlib.numpy.histogram(stabs.values(), bins=6)


'''
# Stability Distribution 
matplotlib.pyplot.plot(h,bins[1:],'s',ls='--')
matplotlib.pyplot.ylabel("Stability")
matplotlib.pyplot.xlabel("hits")
pylab.show()
# Attributes cardinality v/s Stability 
x = []
y = []
for i in stabs:
	x.append(len(i.split(",")))
	y.append(stabs[i])
matplotlib.pyplot.plot(x,y,'o')#,ls='--')
matplotlib.pyplot.ylabel("Stability")
matplotlib.pyplot.xlabel("Attributes Card.")
pylab.show()
'''
''' Attributes cardinality v/s Stability mean '''
points = {}
pointsb = {}
for i in stabs:
	card = len(i.split(","))
	catd = len(lat[i])
	if card not in points:
		points[card] = []
	if catd not in pointsb:
		pointsb[catd] = []
	pointsb[catd].append(stabs[i])
	points[card].append(stabs[i])

for c in points:
	points[c] = np.mean(points[c])
for c in pointsb:
	pointsb[c] = np.mean(pointsb[c])
	
matplotlib.pyplot.semilogx(points.keys(),points.values(),'o')#,ls='--')
matplotlib.pyplot.semilogx(pointsb.keys(),pointsb.values(),'o')#,ls='--')
matplotlib.pyplot.ylabel("Stability")
matplotlib.pyplot.xlabel("Attributes Card.")
pylab.show()
